package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Role;
import com.qk.mapper.RoleMapper;
import com.qk.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class RoleServiceImpl  implements RoleService {
    @Autowired
    private RoleMapper roleMapper;



    @Override
    public PageResult<Role> roleAllList(String name, String label, Integer page, Integer pageSize) {
        //调用pageHelper分页插件
        PageHelper.startPage(page,pageSize);
        List<Role> roles = roleMapper.selectList(name, label);
        Page<Role> p= (Page<Role>) roles;
        return new PageResult<Role>(p.getTotal(),p.getResult());
    }

    @Override
    public void deleteById(Integer id) {
        roleMapper.deleteById(id);
    }

    @Override
    public void addRole(Role role) {
        //补全基础属性
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.insertRole(role);
    }

    @Override
    public Role selectRoleById(Integer id) {
        return roleMapper.selectRoleById(id);
    }

    @Override
    public void updateRole(Role role) {
        //1.补全属性
        role.setUpdateTime(LocalDateTime.now());
        //2.调用update方法
        roleMapper.updateRole(role);
    }

    @Override
    public List<Role> selectAllRole() {
        return roleMapper.selectAllRole();
    }
}
